package it.pecuswap.backend.ejb;

import it.pecuswap.backend.delegate.PecuswapServiceDelegateImpl;
import it.pecuswap.backend.entities.User;
import it.pecuswap.backend.exceptions.ConfigurationException;
import it.pecuswap.backend.exceptions.DelegateException;
import it.pecuswap.backend.intefaces.PecuswapServiceDelegateInteface;
import it.pecuswap.backend.intefaces.PecuswapServiceInterface;
import it.pecuswap.commons.dto.UserDTO;
import it.pecuswap.commons.ejb.interfaces.PecuswapServiceInterfaceRemote;
import it.pecuswap.commons.exceptions.PecuswapEjbException;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * The Class PecuswapServiceEjb.
 */
@Stateless(mappedName = "PecuswapServiceEjb")
public class PecuswapServiceEjb implements PecuswapServiceInterfaceRemote {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = Logger
	    .getLogger(PecuswapServiceEjb.class);
    
    /** The entity manager. */
    @PersistenceContext(unitName = "pecuswapDatabase")
    private EntityManager entityManager;
    
    /**
     * Default constructor.
     */
    public PecuswapServiceEjb() {
    }

    /**
     * Gets the entity manager.
     *
     * @return the entity manager
     */
    public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Sets the entity manager.
	 *
	 * @param entityManager the new entity manager
	 */
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	/**
	 * Configure.
	 *
	 * @throws PecuswapEjbException 
	 */
    @PostConstruct
    public void configure() throws PecuswapEjbException{
	try {
	    // Configuro il logger.
	    LOGGER.info("Configuro il Componente.");
	    String log4jFileReference = Configuration.getInstance().getValueOf(
		    "log4j.configuration.file.reference");
	    PropertyConfigurator.configure(log4jFileReference);

	    LOGGER.info("Componente PecuswapServiceEjb avviato.");
	} catch (ConfigurationException e) {
	    LOGGER
		    .error("Errore nell'acquisizione del file di configurazione del componente.");
	    throw new PecuswapEjbException(e.getMessage());
	}
    }

    /* 
     * find User by userName
     * 
     */
    //@Override
    public UserDTO findUserByUserName(String userName) throws PecuswapEjbException {
		try {
		    PecuswapServiceDelegateInteface delegate = new PecuswapServiceDelegateImpl(
			    this.entityManager);
		    UserDTO userDTO = null; 
		    User user = delegate.findUserByUserName(userName);
		    if (user!=null){
		    	userDTO = new UserDTO();
		    	userDTO = copyFromEntity(userDTO, user);
		    }
		    return userDTO;
		} catch (DelegateException e) {
		    LOGGER.error("errore nel metodo findUserById");
		    throw new PecuswapEjbException(e.getMessage(), e);
		}
    }
    
    private UserDTO copyFromEntity(UserDTO userDTO, User user) throws PecuswapEjbException {
    	if (user!=null && userDTO!=null){
    		userDTO.setId(user.getId());
    		userDTO.setUserName(user.getUserName());
    		userDTO.setPassword(user.getPassword());
    		userDTO.setFirstName(user.getFirstName());
    		userDTO.setLastName(user.getLastName());
    		userDTO.setDtOraInserimento(user.getDtOraInserimento());
    		userDTO.setDtOraUltMod(user.getDtOraUltMod());
	    }
    	return userDTO;
    }
    
}